//
//  XIBrush.m
//  XI Engine
//
//  Created by Balazs Faludi on 31.05.09.
//  Copyright 2009 Balazs Faludi. All rights reserved.
//

#import "XIBrush.h"
//#import <OpenGLES/EAGLDrawable.h>
//#import <OpenGLES/ES1/gl.h>

#import "XIEngine.h"

@implementation XIBrush


+ (void)drawRect:(CGRect)rect withColor:(XIColor *)color
{
	GLfloat squareVertices[] = {
		rect.origin.x, rect.origin.y,
		rect.origin.x + rect.size.width, rect.origin.y,
		rect.origin.x + rect.size.width, rect.origin.y + rect.size.height,
		rect.origin.x, rect.origin.y + rect.size.height,
	};
#if TARGET_OS_IPHONE
	GLfloat squareColors[] = {
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
	};
#else
	GLfloat squareColors[] = {
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
	};
#endif
	glDisable(GL_TEXTURE_2D);
	glVertexPointer(2, GL_FLOAT, 0, squareVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glColorPointer(4, GL_FLOAT, 0, squareColors);
	glEnableClientState(GL_COLOR_ARRAY);
	glDrawArrays(GL_LINE_LOOP, 0, 4);
	glDisableClientState(GL_COLOR_ARRAY);
	glEnable(GL_TEXTURE_2D);
}

+ (void)drawCrossAtPoint:(CGPoint)point withColor:(XIColor *)color
{
	GLfloat squareVertices[] = {
		point.x - kXICrossSize, point.y - kXICrossSize,
		point.x + kXICrossSize, point.y + kXICrossSize,
		point.x - kXICrossSize, point.y + kXICrossSize,
		point.x + kXICrossSize, point.y - kXICrossSize,
	};
#if TARGET_OS_IPHONE
	GLfloat squareColors[] = {
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
		CGColorGetComponents(color.CGColor)[0], CGColorGetComponents(color.CGColor)[1], CGColorGetComponents(color.CGColor)[2], CGColorGetComponents(color.CGColor)[3],
	};
#else
	GLfloat squareColors[] = {
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
		[color redComponent], [color greenComponent], [color blueComponent], [color alphaComponent],
	};
#endif
	glDisable(GL_TEXTURE_2D);
	glVertexPointer(2, GL_FLOAT, 0, squareVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glColorPointer(4, GL_FLOAT, 0, squareColors);
	glEnableClientState(GL_COLOR_ARRAY);
	glDrawArrays(GL_LINES, 0, 4);
	glDisableClientState(GL_COLOR_ARRAY);
	glEnable(GL_TEXTURE_2D);
}

+ (void)drawCircleAtPoint:(CGPoint)point withRadius:(float)radius withColor:(XIColor *)color
{
	GLfloat vertices[720];
	for (int i = 0; i < 720; i += 2) {
		// x value
		vertices[i]   = (cos(DEGREES_TO_RADIANS(i)) * 1);
		// y value
		vertices[i+1] = (sin(DEGREES_TO_RADIANS(i)) * 1);
	}
	glDisable(GL_TEXTURE_2D);
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
	glEnable(GL_TEXTURE_2D);
}


@end
